(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.buCtrlEditController', buCtrlEditController);
	buCtrlEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService' ];
	function buCtrlEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("buCtrlEditPopup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.currBuCtrlCat = ctrl.dialogsModule.params.currBuCtrlCat;
		ctrl.currBuCtrlProdFlag = ctrl.dialogsModule.params.currBuCtrlProdFlag;
		ctrl.buCtrl = ctrl.dialogsModule.params.buCtrl || {};
		
		ctrl.init = function() {
			// reset config for lookup buCtrlData
			var dialogsModule;
			var ctrlDataLabel;
			if (ctrl.currBuCtrlCat == 'COMPANY') {
				dialogsModule = 'cda.company';
				ctrlDataLabel = 'label.bu.buctrldata.company';
			} else if (ctrl.currBuCtrlCat == 'COMPANY_TYPE') {
				dialogsModule = 'cda.companyType';
				ctrlDataLabel = 'label.bu.buctrldata.companytype';
			} else if (ctrl.currBuCtrlCat == 'COMPANY_CLASS') {
				dialogsModule = 'cda.companyClass';
				ctrlDataLabel = 'label.bu.buctrldata.companyclass';
			} else if (ctrl.currBuCtrlCat == 'SEGMENT') {
				dialogsModule = 'cda.segment';
				ctrlDataLabel = 'label.bu.buctrldata.segment';
			} else if (ctrl.currBuCtrlCat == 'INDUSTRY') {
				dialogsModule = 'cda.industry';
				ctrlDataLabel = 'label.bu.buctrldata.industry';
			} else if (ctrl.currBuCtrlCat == 'COUNTRY') {
				dialogsModule = 'cda.country';
				ctrlDataLabel = 'label.bu.buctrldata.country';
			}
			ctrl.layoutConfig.formConfigs.general.gridConfigs.buCtrlData.options.id = dialogsModule;
			ctrl.layoutConfig.formConfigs.general.gridConfigs.buCtrlData.label = ctrlDataLabel;
			//
			ctrl.prodInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.ctrlProdCfg.tableConfigs.table, ctrlProdRowSelectionChanged);
			ctrl.prodInnerGridOptions.data = 'vm.buCtrl.buCtrlProds';
			authService.eidtCtrl().then(function(response) {
				ctrl.buCtrl = ctrl.dialogsModule.params.buCtrl || {};
				ctrl.buCtrl.buCtrlProds = ctrl.buCtrl.buCtrlProds || [];
				ctrl.buCtrl.existBuCtrlDatas = ctrl.dialogsModule.params.buCtrlDatas;// for lookup filter
				ctrl.buCtrl.ctrlDataLabel = ctrlDataLabel;
			}, angular.noop);
		};
		
		$scope.$on("BUCTRLDATA_OK", function(event, param) {
			var returnEntities = param.returnEntities;
			if (!returnEntities || returnEntities.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return false;
			}
			if (ctrl.currBuCtrlCat == 'COMPANY') {
				ctrl.buCtrl.buCtrlData = returnEntities[0].companyCode;
			} else if (ctrl.currBuCtrlCat == 'COMPANY_TYPE') {
				ctrl.buCtrl.buCtrlData = returnEntities[0].companyType;
			} else if (ctrl.currBuCtrlCat == 'COMPANY_CLASS') {
				ctrl.buCtrl.buCtrlData = returnEntities[0].companyClass;
			} else if (ctrl.currBuCtrlCat == 'SEGMENT') {
				ctrl.buCtrl.buCtrlData = returnEntities[0].segmentCode;
			} else if (ctrl.currBuCtrlCat == 'INDUSTRY') {
				ctrl.buCtrl.buCtrlData = returnEntities[0].industryCode;
			} else if (ctrl.currBuCtrlCat == 'COUNTRY') {
				ctrl.buCtrl.buCtrlData = returnEntities[0].countryCode;
			}
			//
			event.preventDefault();
			event.stopPropagation();
		});
		
		$scope.$on("BUCTRLDATA_CANCEL", function(event, param) {
			event.preventDefault();
			event.stopPropagation();
		});
		
		ctrl.addCtrlProd = function() {
			var params = {};
			params.filterProdTypes = [];
			angular.forEach(ctrl.buCtrl.buCtrlProds, function(prodTypeItem, index) {
				params.filterProdTypes.push(prodTypeItem.prodType);
			});
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.prodtype',
				'params' : params,
				'btnCallback' : {
					'ok' : function(selectedRows) {
						if (!selectedRows || selectedRows.length == 0) {
							modalService.openErrorDialogs({
								messages : [ 'title.dialogs.info.norecsel' ]
							});
							return false;
						}
						if (!ctrl.buCtrl.buCtrlProds) {
							ctrl.buCtrl.buCtrlProds = [];
						}
						selectedRows[0].dataStatus = 'C';
						selectedRows[0].prodType = selectedRows[0].prodType;
						ctrl.buCtrl.buCtrlProds.push(selectedRows[0]);
						// reset ctrl.buCtrl.prodTypeShow
						resetProdTypeForShow(ctrl.buCtrl.buCtrlProds);
					}
				}
			});
		};

		ctrl.removeCtrlProd = function() {
			angular.forEach(ctrl.selectedCtrlProds, function(selectedCtrlProd, key) {
				angular.forEach(ctrl.buCtrl.buCtrlProds, function(prodTypeItem, index) {
					if (prodTypeItem.prodType == selectedCtrlProd.prodType) {
						if (prodTypeItem.dataStatus =='C') {
							ctrl.buCtrl.buCtrlProds.splice(index, 1);
						} else {
							prodTypeItem.dataStatus = 'D';
						}
					}
				});
			});
			// reset ctrl.buCtrl.prodTypeShow
			resetProdTypeForShow(ctrl.buCtrl.buCtrlProds);
		};
		
		function resetProdTypeForShow (buCtrlProds) {
			var prodTypeShow = "";
			if (buCtrlProds && buCtrlProds.length > 0) {
				for (var i = 0; i < buCtrlProds.length; i++) {
					if (buCtrlProds[i].dataStatus !== "D") {
						prodTypeShow = prodTypeShow + buCtrlProds[i].prodType + ",";
					}
				}
			}
			if (prodTypeShow && prodTypeShow.length > 0) {
				prodTypeShow = prodTypeShow.substr(0, prodTypeShow.length - 1);
			}
			ctrl.buCtrl.prodTypeShow = prodTypeShow;
		}
		
		function ctrlProdRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedCtrlProds = selectedRows;
		}
		
		ctrl.ok = function() {
			if (!ctrl.buCtrl.buCtrlData) {
				modalService.openErrorDialogs({
					messages : [ 'please input BU Control Data first!' ]
				});
				return;
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.buCtrl) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);